package at.spengergasse.dao.derby;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.sql.Connection;
import java.util.Date;
import java.util.List;

import org.junit.BeforeClass;
import org.junit.Test;

import at.spengergasse.dao.PlzDao;
import at.spengergasse.dao.util.JDBCConnectionController;
import at.spengergasse.model.Plz;


public class PlzDaoDerbyImplTest {
	
	private static Connection connection;
	private static PlzDao dao;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		connection = JDBCConnectionController.getConnection();
		dao = new PlzDAODerbyImpl();
	}
	
	  @Test
	  public void testFindById() {
	    Plz plz = dao.findById(connection, Long.valueOf(5));
	    assertEquals("Baden", plz.getOrt());
	    assertEquals(2500, plz.getPlz());
	  }
	  
	  @Test
	  public void testFindByIdWithNonExistingId() {
	    Plz plz = dao.findById(connection, Long.valueOf(999));
	    assertNull(plz);
	  }
	  
	  @Test
	  public void testDelete() {
	    dao.save(connection, new Plz(1000, "testPlz"));
	    List<Plz> listPlz = dao.findAll(connection);
	    assertEquals(11, listPlz.size());
	    Plz plz = listPlz.get(5);
	    dao.delete(connection, plz);
	    List<Plz> rereadPlz = dao.findAll(connection);
	    assertEquals(10, rereadPlz.size());
	  }
	
	@Test
	public void testSaveNew() {
		Plz newPlz = new Plz(3331, "Hilm");
		int numberOfRecords = dao.save(connection, newPlz);
		List<Plz> plz = dao.findByPlz(connection, 3331);
		assertEquals(1, numberOfRecords);
		assertNotNull(newPlz.getId());
		assertNotNull(newPlz.getLastModifiedTS());
		assertEquals("Hilm", plz.get(0).getOrt());
		List<Plz> plzs = dao.findAll(connection);
		assertEquals(11, plzs.size());
	}
	
	  @Test
	  public void testSaveExisting() {
	    Plz existingPlz = dao.findById(connection, (long)1);
	    existingPlz.setOrt("newOrt");
	    Date lastModTS = existingPlz.getLastModifiedTS();
	    dao.save(connection, existingPlz);
	    assertFalse(lastModTS.equals(existingPlz.getLastModifiedTS()));
	    Plz checkPlz = dao.findById(connection, Long.valueOf(1));
	    assertEquals("newOrt", checkPlz.getOrt());
	  }
	  
	  @Test
	  public void testFindAll() {
	    List<Plz> listPlz = dao.findAll(connection);
	    assertEquals(11, listPlz.size());
	    assertEquals("newOrt", listPlz.get(0).getOrt());
	  }

	  @Test
	  public void testFindByNamePart() {
	    List<Plz> listPlz = dao.findByNamePart(connection, "W");
	    assertEquals(2, listPlz.size());
	    assertEquals("Wolkersdorf", listPlz.get(0).getOrt());
	    assertEquals("Wien", listPlz.get(1).getOrt());
	    assertEquals(2120, listPlz.get(0).getPlz());
	  }

	  @Test
	  public void testFindByNamePartWithNotExistingNamePart() {
	    List<Plz> listPlz = dao.findByNamePart(connection, "Maria%");
	    assertEquals(0, listPlz.size());
	  }
	  
	  @Test
	  public void testFindByPlz() {
	    List<Plz> listPlz = dao.findByPlz(connection, 1110);
	    assertEquals("newOrt", listPlz.get(0).getOrt());
	  }
}
